package com.shiftychess.piece
{
	import com.shiftychess.board.Colour;
	import com.shiftychess.board.Position;
	import com.shiftychess.model.GameModel;
	
	[Bindable]
	[RemoteClass(alias="com.shiftychess.model.piece.Queen")]
	public class Queen implements IChessPiece {
		public var pieceId:int;
		public var type:String;
		public var colour:String;
		public var positionX:String;
		public var positionY:int;
		
		public function Queen() {
		}
		
		public function get position():Position {
			var pos:Position = new Position();
			pos.x = positionX;
			pos.y = positionY;
			
			return pos;
		}
						
		public function canMove(position:Position):Boolean {
			
			var currentX:int = Position.convertXtoInt(positionX);
			var requestX:int = Position.convertXtoInt(position.x);
			var difX:int = Math.abs(currentX - requestX);
			var currentY:int = positionY;
			var requestY:int = position.y;
			var difY:int = Math.abs(currentY - requestY);
			
			//move only in the same direction, can take piece, can not jump over piece
			if(difX == 0 || difY == 0) {
				if(GameModel.checkEmptySpace(position)) {
					return GameModel.checkEmptySpaceBetween(this.position, position);
				} else if(GameModel.checkEnemySpace(position, this.colour)) {
					return GameModel.checkEmptySpaceBetween(this.position, position);
				} else {
					return false;
				}
			} else if(difX == difY) {
				if(GameModel.checkEmptySpace(position)) {
					return GameModel.checkEmptySpaceBetween(this.position, position);
				} else if(GameModel.checkEnemySpace(position, this.colour)) {
					return GameModel.checkEmptySpaceBetween(this.position, position);
				} else {
					return false;
				}
			} else {
				return false
			}
		}
		
		public function move(position:Position):void {
			//update position
			positionX = position.x;
			positionY = position.y;
			
			//update board
			GameModel.movePiece(pieceId, position);
			
			//check or checkmate?
			GameModel.isCheckMate();
		}
	}
}